package com.wyh.controller;

import com.wyh.Mapper.DepartmentMapper;
import com.wyh.Mapper.EmployeeMapper;
import com.wyh.pojo.Department;
import com.wyh.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    @RequestMapping("/main")
    public String main(){
        return "/dashboard";
    }

    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/emps")
    public String list(Model model){
        List<Employee> all = employeeMapper.getEmployeeList();

        model.addAttribute("emps",all);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //查出部门的信息
        Collection<Department> departments = departmentMapper.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        //添加的操作
        System.out.println(employee);
        employee.setDepartment(departmentMapper.getDepartmentById(employee.getDepartment().getDeptId()));
        employeeMapper.addEmployee(employee);
        return "redirect:/emps";
    }

    @GetMapping("/toEditEmp/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model){
        //查出部门信息传给前台
        List<Department> departments = departmentMapper.getDepartments();
        model.addAttribute("departments",departments);
        System.out.println("id:"+id);
        Employee employee = employeeMapper.getEmployeeById(id);
        System.out.println(employee);
        model.addAttribute("emp",employee);
        return "emp/update";
    }

    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee){
        System.out.println(employee);
        System.out.println(employee.getBirthday());
        employeeMapper.updateEmployee(employee);
        return "redirect:/emps";
    }

    @GetMapping("/deleteEmp/{id}")
    public String delete(@PathVariable("id") Integer id){
        employeeMapper.deleteEmployee(id);
        return "redirect:/emps";
    }


    @RequestMapping("/searchEmp")
    public String searchEmp(String name, Model model){
        List<Employee> employeeList = employeeMapper.getEmployeeByName(name);
        System.out.println("================search");
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
        model.addAttribute("emps",employeeList);
        return "emp/list";
    }
    @RequestMapping("/showAgeMap")
    public String showMap(){
        return "map/map";
    }
}
